<?php
include("funciones/setup.php");
conecta();

session_start();

if(isset($_SESSION['sesion_usuario']))
{
	$id=$_GET['id'];
	if(isset($id))
	{
		$sql="select * from guias where id_guia=".$id;
		$reg=mysql_query($sql);
		$datos=mysql_fetch_array($reg);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Formulario Guia</title>
<link href="css/estilos_forms.css" rel="stylesheet" type="text/css" />
<link href="css/kendo.common.css" rel="stylesheet"/>
<link href="css/kendo.default.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="css/paginador_busqueda.css"/>

<link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.custom.css">
<link rel="stylesheet" type="text/css" href="css/autocomplete.css"/>
<link rel="stylesheet" type="text/css" media="screen"  href="css/calendario.css"> 
<link rel="stylesheet" type="text/css" media="screen" href="css/datePicker.css">
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script src="js/guia.js" language="javascript" type="text/javascript"></script>
<script src="js/kendo.core.js" language="javascript" type="text/javascript"></script>
<script src="js/kendo.numerictextbox.js" language="javascript" type="text/javascript"></script>
<script src="js/kendo.validator.js" language="javascript" type="text/javascript"></script>
<script src="js/kendo.combobox.js" language="javascript" type="text/javascript"></script>

<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script src="js/date.js" type="text/javascript"></script>
<script src="js/jquery.datePicker.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function lookup(inputString) {
		if(inputString.length == 0) {
			$('#suggestions').hide();
		} else {
			$.post("busqueda_proveedores.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup
	function lookup2(inputString) {
		if(inputString.length == 0) {
			$('#suggestions2').hide();
		} else {
			$.post("busqueda_productos.php?id=<?php echo $datos['id_pro']; ?>", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions2').show();
					$('#autoSuggestionsList2').html(data);
				}
			});
		}
	} // lookup
	
	function fill(thisValue,thisValue2) {
		$('#frm_bus_provedor').val(thisValue2);
		$("#id_proveedor").val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	}
		function fill2(thisValue,thisValue2) {
		$('#frm_producto').val(thisValue2);
		$("#id_producto").val(thisValue);
		setTimeout("$('#suggestions2').hide();", 200);
	}				
</script>
<style type="text/css">
	.suggestionsBox {
		position: relative;
		left: 30px;
		margin: 10px 0px 0px 0px;
		width: 200px;
		background-color: #212427;
		-moz-border-radius: 7px;
		-webkit-border-radius: 7px;
		border: 2px solid #000;	
		color: #fff;
	}
	
	.suggestionList {
		margin: 0px;
		padding: 0px;
	}
	
	.suggestionList li {
		
		margin: 0px 0px 3px 0px;
		list-style-type:none;
		padding: 3px;
		cursor: pointer;
	}
	
	.suggestionList li:hover {
		background-color: #659CD8;
	}
</style>
<link href="css/estilos_base.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td><div id="credenciales">
      <table width="100%" border="0" cellspacing="3" cellpadding="0">
        <tr>
          <td width="42"><img src="imagenes/iconos/credenciales.png" width="42" height="28" /></td>
          <td class="fuente_normal">Bienvenido : <?php echo $_SESSION['sesion_usuario'];?></td>
        </tr>
        <tr>
          <td colspan="2"><table border="0" align="center" cellpadding="0" cellspacing="3">
            <tr>
              <td><a href="cerrar_sesion.php"><img src="imagenes/iconos/salir_session.png" width="22" height="23" border="0" /></a></td>
              <td class="fuente_normal"><a href="cerrar_sesion.php">Cerrar Sessión</a></td>
              <td class="fuente_normal">|</td>
              <td class="fuente_normal"><a href="panel_principal.php"><img src="imagenes/iconos/ico_panel_principal.png" width="22" height="23" border="0" /></a></td>
              <td class="fuente_normal"><a href="panel_principal.php">Panel Principal</a></td>
            </tr>
          </table></td>
        </tr>
      </table>
    </div></td>
    <td><div id="logotipo"><img src="imagenes/logo.png" width="200" height="82" /></div></td>
    <td><table border="0" align="right" cellpadding="0" cellspacing="5">
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><span class="fuente_login">La Serena, <?php echo fecha_hoy_mas_dia();?>&nbsp;</span></td>
      </tr>
    </table></td>
  </tr>
</table>
<br />
<div id="modulo">
  <form id="form1" name="form1" method="post" action="">
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">
      <tr>
        <td width="141" class="fuente_formularios">Numero Guia :</td>
        <td width="227" class="fuente_formularios"><input name="frm_guia" type="text" class="k-textbox" id="frm_guia" value="<?php echo $datos['n_guia'];?>" size="33" placeholder="Ingrese Numero de Guia" required/></td>
        <td width="99" class="fuente_formularios">Fecha :</td>
        <td class="fuente_formularios"><input name="frm_fecha" type="text" class="k-textbox date-pick" id="frm_fecha" value="<?php if(isset($id)) { echo fecha_formato_espanol_gore($datos['fecha_crea']);  } ?>" size="12"   required/></td>
      </tr>
      <tr>
        <td class="fuente_formularios">Proveedor :</td>
        <td colspan="3" class="fuente_formularios">
          <?php
		 if(isset($id)){ 
			 $sql_prov="select * from proveedor where id_pro=".$datos['id_pro'];
			 $result_prov=mysql_query($sql_prov);
			 $row_prov=mysql_fetch_array($result_prov);
		 }
		
		?>
          <input type="text" size="30" value="<?php  echo $row_prov['nombre_pro'];?>" class="k-textbox" id="frm_bus_provedor" onkeyup="lookup(this.value);" onblur="fill();" placeholder="Buscar Proveedor" required<?php  if(isset($id)){ ?> disabled="disabled"<?php } ?>/>
          <input type="hidden" name="id_proveedor" id="id_proveedor" value="<?php echo $datos['id_pro']; ?>" />
  <div class="suggestionsBox" id="suggestions" style="display: none;">
    <img src="imagenes/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
    <div class="suggestionList" id="autoSuggestionsList">
      &nbsp;
      </div>
    </div>
        </td>
      </tr>
      <?php if(isset($id)){ ?>
      <tr class="fuente_formularios">
        <td>Agregar Producto :</td>
        <td colspan="3"><input name="frm_producto" type="text" class="k-textbox" id="frm_producto" value="<?php echo $datos['descripcion'];?>" size="35" onkeyup="lookup2(this.value);" onblur="fill2();" placeholder="Buscar Producto para Ingresar" required/>
          <input type="hidden" name="id_producto" id="id_producto" />
          <div class="suggestionsBox" id="suggestions2" style="display: none;">
            <img src="imagenes/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList2">
              &nbsp;
            </div>
        </div></td>
      </tr>
      <tr class="fuente_formularios">
        <td rowspan="3">Observación Producto :</td>
        <td rowspan="3"><textarea name="frm_descripcion" id="frm_descripcion" cols="20" rows="5"></textarea></td>
        <td>Estado :</td>
        <td><label for="frm_estado_pro"></label>
          <select name="frm_estado_pro" id="frm_estado_pro">
            <option value="1">Incompleto</option>
            <option value="2">Vencido</option>
            <option value="3">Con Detalle</option>
            <option value="4">Otro</option>
        </select></td>
      </tr>
      <tr class="fuente_formularios">
        <td>Cant:</td>
        <td width="56"><label for="frm_cantidad"></label>
        <input name="frm_cantidad" type="text" id="frm_cantidad" class="k-textbox"  size="10" /></td>
      </tr>
      <tr class="fuente_formularios">
        <td>Valor Compra :</td>
        <td><input name="frm_valor" type="text" id="frm_valor" class="k-textbox"  size="10" />
        <input type="button" name="btn_addproducto" id="btn_addproducto" value="+" class="k-button"/></td>
      </tr>
      <?php }?>
      <tr>
        <td colspan="4"><div id="proveedores"></div></td>
      </tr>
      <tr>
        <td colspan="4"><center>
          <label for="oculto_id"></label>
          <input type="hidden" name="id_oculto" id="id_oculto" value="<?php echo $id;?>" />
          <?php
		    if(!isset($id))
			{
				?>
          <input type="button" name="btn_ingresar" id="btn_ingresar" value="Ingresar" class="k-button"/> 
          &nbsp;
          <?php
			}
			else
			{?>
          <input type="button" name="btn_modificar" id="btn_modificar" value="Modificar" class="k-button"/>
          &nbsp;
          <input type="button" name="btn_eliminar" id="btn_eliminar" value="Eliminar" class="k-button"/>
          &nbsp;
          <?php
			}
			?>
          <input type="button" name="btn_ingresar4" id="btn_cancelar" value="Cancelar" class="k-button"/>
        </center></td>
      </tr>
    </table>
  </form>
</div>
<br />
<div id="paginador"></div>

<script language="javascript">

$(document).ready(function(){
	$("#proveedores").hide();
$("#btn_ingresar").click(function(){
		ingresar();
	});
	
	$("#btn_modificar").click(function(){
		modificar();
	});
	
	$("#btn_eliminar").click(function(){
		elimina();
	});
	
	$("#btn_cancelar").click(function(){
		location.href="guia.php";
	});
	$("#btn_addproducto").click(function(){
		agregar_productos();
		$('#frm_producto').val('');
		$('#frm_cantidad').val('');
		$('#frm_valor').val('');
	});
	$("#frm_margen").kendoNumericTextBox({
        format: "p0",
         min: 0,
         max: 1,
         step: 0.01
    });
	
	$("#frm_neto").kendoNumericTextBox({
         format: "c",
         decimals: 0
    });
	
	$("#frm_stock").kendoNumericTextBox();
	
	$("#paginador").html(muestra_grilla());
	
	$("#proveedores").html(muestra_grilla_pro(<?php echo $id; ?>));
  	<?php  if(isset($id)){ ?>$("#proveedores").show();<?php } ?>
	$('.date-pick').datePicker({
	 startDate:'01/01/2000',
    clickInput:true
   }).val(<? if(!isset($id)){ ?>new Date().asString()<? }?>);

});

</script>
</body>
</html>
<?php
}
else
{
	header("Location:index.php");
}
?>